<template>
  <van-pull-refresh v-model="refreshing" @refresh="onRefresh" class="order-wrap">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      error-text="请求失败，点击重新加载"
      @load="onLoad"
      class="order-list-wrap"
    >

      <div v-for="(order, i) in list" :key="i" @click.self="onClick(order)">
        <div class="order-list">
          <div class="shop-name">
            <div>
              <van-icon name="shop-o" size="18"/>
              <span>雾缘蒸汽</span></div>
            <span class="order-status"> {{ handleStatusText(order.orderStatus) }}</span>
          </div>
          <van-divider/>
          <div class="card-item" v-for="(item, index) in order.goodsList" :key="index" @click="openDetail(item,order)">
            <div class="order-item">
              <img
                class="order-thumb"
                v-lazy="item.picUrl"
              />
              <div class="order-info">
                <div class="order-title">
                  <span>{{ item.goodsName }}</span>
                </div>
                <div class="order-type">
                  <span>{{ item.specifications.join(',') }}</span>
                </div>
                <div class="order-counts">
                  <span class="order-price">¥{{ item.price }}</span>

                  <van-stepper v-if="item.isEdit" v-model="item.number"
                               disableInput/>
                  <div v-else>
                    <span class="order-count">x{{ item.number }}</span>
                  </div>
                </div>

              </div>

            </div>
          </div>
          <slot name="footer" v-bind:order="order" v-bind:onDataChange="onDataChange"></slot>
        </div>
      </div>
    </van-list>
  </van-pull-refresh>
</template>

<script>
import {orderList} from '../api/order'

export default {
  name: 'OrderList',
  data () {
    return {
      refreshing: false,
      loading: false,
      finished: false,
      pages: 1,
      page: 0,
      list: []

    }
  },

  props: {
    status: Number,
    onOrderClick: Function
  },
  methods: {
    onClick (order) {
      this.onOrderClick(order)
    },
    handleStatusText (status) {
      if (status === 101) {
        return '未付款'
      }

      if (status === 102) {
        return '已取消'
      }

      if (status === 103) {
        return '已取消(系统)'
      }

      if (status === 201) {
        return '已付款'
      }

      if (status === 202) {
        return '订单取消，退款中'
      }

      if (status === 203) {
        return '已退款'
      }

      if (status === 204) {
        return '已超时团购'
      }

      if (status === 301) {
        return '已发货'
      }

      if (status === 401) {
        return '已收货'
      }

      if (status === 402) {
        return '已收货(系统)'
      }
    },
    onDataChange (id, status) {
      this.list.forEach((item, index) => {
        if (item.id === id) {
          item.orderStatus = status
          this.$set(this.list, index, item)
        }
      })
    },
    openDetail (item, order) {
      if (this.onOrderClick !== undefined) {
        this.onOrderClick(order)
      } else {
        this.$router.push({name: 'detail', params: {id: item.goodsId}})
      }
    },
    onLoad () {
      if (this.page >= this.pages) {
        this.finished = true
      } else {
        orderList({page: this.page, showType: this.status}).then(res => {
          let data = res.data.data
          this.loading = false
          this.pages = data.pages
          data.list.forEach(item => {
            this.list.push(item)
          })
        })
        this.page++
      }
    },
    onRefresh () {
      this.page = 0
      orderList({page: this.page, showType: this.status}).then(res => {
        let data = res.data.data
        this.refreshing = false
        this.pages = data.pages
        this.list = data.list
      })
    }
  }
}
</script>

<style scoped>

.order-wrap {
  background: #f3f3f3;;
  height: 100%;
  display: flex;
  flex-direction: column;
  flex: 1;
}

.order-list-wrap {
  margin-top: 10px;
}

.order-list {
  background: white;
  display: flex;
  flex-direction: column;
  margin: 0 5px 5px 5px;
  padding: 5px;
  border-radius: 5px;
  box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
}

.order-item {
  margin-bottom: 10px;
  display: flex;
  flex-direction: row;
}

.order-thumb {
  border-radius: 3px;
  width: 80px;
  height: 80px;
}

.order-info {
  flex: 1;
  margin-left: 5px;
}

.order-title {
  font-size: 16px;
  font-weight: bold;

}

.order-type {
  margin: 10px 0;
  width: max-content;
  color: gray;
  font-size: 10px;
}

.order-counts {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}

.order-price {
  color: #E65D6E;
  font-size: 16px;
}

.order-counts {
  font-size: 12px;
}
</style>
